<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="分公司">
        <el-select v-model="formInline.branchId" v-if="isBranch" style="width: 180px" filterable>
          <el-option
            v-for="item in branchList"
            :label="item.branch_Name"
            :value="item.branch_Id">
          </el-option>
        </el-select>
        <el-input v-if="!isBranch" v-model="branchName" disabled></el-input>
      </el-form-item>
      <!--<el-form-item label="所属部门">-->
        <!--<el-select v-model="formInline.departmentId" v-if="isBranch" style="width: 180px" filterable>-->
          <!--<el-option-->
            <!--v-for="item in deptList"-->
            <!--:value="item.org_id"-->
            <!--:label="item.org_name">-->
          <!--</el-option>-->
        <!--</el-select>-->
        <!--<el-input v-if="!isBranch" v-model="orgName" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="分公司">-->
        <!--<el-input v-model="this.branchName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属部门">-->
        <!--<el-input v-model="this.orgName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="用户名">
        <el-input v-model="formInline.operator"></el-input>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          :editable="false"
          v-model="formInline.time"
          value-format="yyyy-MM-dd"
          type="daterange"
          @change="timeChange"
          placeholder="选择日期范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="功能">
        <el-select v-model="formInline.category" @change="pageChange" style="width: 180px" filterable>
          <el-option
            v-for="item in stateList"
            :label="item.categoryName"
            :value="item.category">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      border>
      <el-table-column
        show-overflow-tooltip
        prop="branchName"
        label="分公司"
        align="center"
        width="">
      </el-table-column>
      <el-table-column
        prop="operator"
        label="用户名"
        align="center"
        width="">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="categoryName"
        label="功能"
        width=""
        align="center">
      </el-table-column>
      <el-table-column
        prop="actionName"
        label="操作类型"
        align="center"
        show-overflow-tooltip
        width="">
      </el-table-column>
      <el-table-column
        prop="actionTime"
        label="操作时间"
        show-overflow-tooltip
        align="center"
        width="">
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(pageData.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <!--详情弹窗-->
    <!--<el-dialog title="费用详情" :visible.sync="detailDialog" top="10%" class="detail-info">-->
      <!--<div class="form-item" v-if="detailData.detail.promotionType === 1">-->
        <!--<pre style="margin: -10px;line-height: 28px">-->
          <!--分公司：   {{detailData.detail.branchName}}-->
          <!--所属部门： {{detailData.detail.departmentName}}-->
          <!--促销费用名称：{{detailData.detail.promotionName}}-->
          <!--费用类型：{{detailData.detail.promotionTypeName}}-->
          <!--供应商：{{detailData.detail.supplyFactory}}-->
          <!--促销周期：  {{detailData.detail.promotionBeginTime}}至{{detailData.detail.promotionEndTime}}-->
        <!--</pre>-->
      <!--</div>-->
      <!--<div class="form-item" v-else>-->
        <!--<pre style="margin: -10px;line-height: 28px">-->
          <!--分公司：   {{detailData.detail.branchName}}-->
          <!--所属部门： {{detailData.detail.departmentName}}-->
          <!--促销费用名称：{{detailData.detail.promotionName}}-->
          <!--费用类型：{{detailData.detail.promotionTypeName}}-->
          <!--供应商：{{detailData.detail.supplyFactory}}-->
          <!--促销周期：  {{detailData.detail.promotionBeginTime}}至{{detailData.detail.promotionEndTime}}-->
          <!--补偿方式：  {{detailData.detail.payModeName}}-->
          <!--补偿周期：  {{detailData.detail.payBeginTime}}至{{detailData.detail.payEndTime}}-->
        <!--</pre>-->
      <!--</div>-->
      <!--<div class="form-item">-->
        <!--<h4>备注</h4>-->
        <!--<el-form label-width="180px">-->
            <!--<el-input  type="textarea" disabled placeholder="请设置活动说明" v-model="detailData.detail.remark" style="width: 500px"></el-input>-->
        <!--</el-form>-->
      <!--</div>-->
    <!--</el-dialog>-->
  </div>
</template>
<script>
  import { getInfo, czrz, activity } from '../../../config/proxy.js'
  // import { mapState } from 'vuex'
  import mixins from '../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        stateList: [],
        deptList: [],
        visible: false,
        formInline: {
          branchId: '',
          departmentId: '',
          endTime: '',
          beginTime: '',
          category: '',
          operator: ''
        },
        tableData: [],
        isLoading: false,
        pageData: {
          pageSize: 10,
          page: 1
        },
        total: '',
//      详情弹窗
        detailDialog: false,
        detailData: {
          detail: '',
          tableData: []
        }
      }
    },
    methods: {
      pageChange () {
        this.pageData.page = 1
      },
      getDeptList (val) {
        if (this.visible === true) {
          this.formInline.departmentId = ''
        }
        var _data = {}
        this.branchList.forEach(item => {
          if (val === item.branch_Id) {
            _data.orgId = item.org_id
          }
        })
        getInfo.department(_data, (res) => {
          if (res.message === 'ok') {
            if (val === 1) {
              this.deptList = []
              res.content.forEach(element => {
                if (element.org_name.indexOf('公司') === -1) {
                  this.deptList.push(element)
                }
              })
              this.matchDept()
            } else {
              this.deptList = res.content
              this.matchDept()
            }
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },  // 获取部门
      matchDept () { // 匹配 当前登录人的部门与部门列表
        for (let index = 0; index < this.deptList.length; index++) {
          const element = this.deptList[index]
          if (Number(this.formInline.departmentId) === Number(element.org_id)) {
            return
          } else {
            if (index === this.deptList.length - 1) {
              this.formInline.departmentId = ''
            }
          }
        }
      },
      visibleChange (val) {
        this.visible = val
      },
      fyCreation () {
        this.openNewWindow('fy_add', '新增促销费用', 'fy_add')
      },
      handle (index, row, event) {
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            this.detailDialog = true
            activity.cxfy.detail({id: row.id}, (res) => {
              if (res.message === 'ok') {
                this.detailData.detail = res.content
                this.detailDialog = true
              } else {
                this.$message({
                  showClose: true,
                  message: res.message,
                  type: 'error'
                })
              }
            })
            break
          case '删除':
            this.$confirm('您确定要删除此活动费用？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              activity.cxfy.delete({id: row.id}, (res) => {
                if (res.message === 'ok') {
                  this.onSubmit()
                  this.$message({
                    type: 'success',
                    message: '删除成功'
                  })
                } else {
                  this.$message({
                    type: 'error',
                    message: res.message
                  })
                }
              })
            }).catch(() => {
            })
            break
          case '编辑':
            this.openNewWindow('fy_edit', '编辑促销费用', 'fy_add', row)
            break
        }
      },    // 操作
      timeChange (val) {
        if (val) {
          this.formInline.beginTime = val[0]
          this.formInline.endTime = val[1]
        }
      },
      onSubmit () {
        this.isLoading = true
        var _data = Object.assign(this.formInline, this.pageData)
        czrz.query(_data, (res) => {
          if (res.message === 'ok') {
            this.tableData = res.content.content
            this.total = res.content.count
            this.pageData.page = res.content.page
            this.isLoading = false
          } else {
            this.isLoading = false
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },                  // 提交
      handleCurrentChange (val) {
        this.pageData.page = val
        this.onSubmit()
      }    // 分页
    },
    computed: {
      orgName: function (state) {
        return this.$store.state.user.info.parOrgName
      },
      orgId: function (state) {
        return this.$store.state.user.info.or_org_id
      },
      branchName: function (state) {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function (state) {
        return this.$store.state.user.info.branchId
      },
      update_state: function () {
        return this.$store.state.updateState.cxfyState
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.onSubmit()
      },
      branchList (a, b) {
        this.getDeptList(this.formInline.branchId)
      }
    },
    created () {
      czrz.list({}, (res) => {
        this.stateList = res.content
      })
      this.formInline.branchId = this.branchId
      this.onSubmit()
    }
  }
</script>
<style>
</style>
